﻿using QFramework;
using UnityEngine;

namespace ProjectIndieFarm
{
    public class ToolSeed : ITool, IController
    {
        public string Name => "seed";

        public int Range => Global.PlantActionRange;

        public ToolbarSlot Slot { get; set; }

        public float TimeCost => Global.ToolHourCostSeed;

        public float ActionDuration => 0.3f * (Global.ToolHourCostSeed / Config.INIT_TOOL_HOUR_COST_SEED);

        public bool Selectable(ToolData data)
        {
            return Global.CurrentTool.Value.Name.Equals(Name)
                        && Slot.Count.Value > 0
                        && data.SoilGrid[data.CellPosition.x, data.CellPosition.y] != null
                        && data.SoilGrid[data.CellPosition.x, data.CellPosition.y].HasPlant == false;
        }

        public void Use(ToolData data)
        {
            //种子减少
            this.SendCommand(new SubItemCountCommand(Slot.ItemID.Value, 1));

            var itemConfig = Config.ItemForName[Slot.ItemID.Value];
            //种下植物
            var plantGameObj = ResController.Instance.LoadPlantPrefabs(itemConfig.PlantPrefabName)
                .Instantiate().Position(data.WorldPosition);
            var plant = plantGameObj.GetComponent<IPlant>();
            plant.XCell = data.CellPosition.x;
            plant.YCell = data.CellPosition.y;

            //记录植物信息
            PlantController.Instance.PlantGrid[data.CellPosition.x, data.CellPosition.y] = plant;
            data.SoilGrid[data.CellPosition.x, data.CellPosition.y].HasPlant = true;
            data.SoilGrid[data.CellPosition.x, data.CellPosition.y].PlantState = PlantStates.Seed;
            data.SoilGrid[data.CellPosition.x, data.CellPosition.y].PlantName = plant.GetName();

            AudioController.Instance.SfxSeed.Play();

            CameraController.ShakeSlight();
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}
